package com.cmic.rcs.emoji.controller.web;


import com.cmic.rcs.emoji.entity.pojo.TBusinessTopology;
import com.cmic.rcs.emoji.service.BusinessTopologyService;
import com.cmic.rcs.enhance.message.g5.core.common.Response;
import com.cmic.rcs.enhance.message.g5.core.common.ResponseData;
import com.cmic.rcs.enhance.message.g5.core.common.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 业务拓扑图配置控制器
 * 
 * @author 孙高明
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/api/web/topology")
@Slf4j
public class BusinessTopologyController {

    @Autowired
    private BusinessTopologyService businessTopologyService;

    /**
     * 上传拓扑图
     * 
     * @param file 上传的图片文件
     * @param enterpriseCode 企业编码
     * @return 上传结果
     */
    @PostMapping("/upload")
    public Response uploadTopology(@RequestParam("file") MultipartFile file, 
                                  @RequestParam("enterpriseCode") String enterpriseCode) {
        log.info("上传拓扑图，企业编码：{}", enterpriseCode);
        try {
            TBusinessTopology topology = businessTopologyService.uploadTopology(file, enterpriseCode);
            // 构建返回数据
            Map<String, Object> result = new HashMap<>();
            result.put("imageName", topology.getImageName());
            result.put("imageSize", topology.getImageSize());
            result.put("imageUrl", topology.getImageUrl());
            result.put("uploadTime", topology.getUploadTime());
            
            log.info("拓扑图上传成功，企业编码：{}", enterpriseCode);
            return ResponseData.OK(result);
        } catch (RuntimeException e) {
            log.error("拓扑图上传失败，企业编码：{}, 错误信息：{}", enterpriseCode, e.getMessage());
            return ResponseData.Error(ReturnCodeEnum.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 获取企业拓扑图
     * 
     * @param enterpriseCode 企业编码
     * @return 拓扑图信息
     */
    @PostMapping("/get")
    public Response getTopology(@RequestParam("enterpriseCode") String enterpriseCode) {
        log.info("获取企业拓扑图，企业编码：{}", enterpriseCode);
        try {
            TBusinessTopology topology = businessTopologyService.getTopologyByEnterpriseCode(enterpriseCode);
            if (topology == null) {
                log.info("未找到企业拓扑图，企业编码：{}", enterpriseCode);
                return ResponseData.OK(null);
            }
            
            // 构建返回数据
            Map<String, Object> result = new HashMap<>();
            result.put("imageName", topology.getImageName());
            result.put("imageSize", topology.getImageSize());
            result.put("imageUrl", topology.getImageUrl());
            result.put("uploadTime", topology.getUploadTime());
            result.put("imageType", topology.getImageType());
            
            return ResponseData.OK(result);
        } catch (RuntimeException e) {
            log.error("获取企业拓扑图失败，企业编码：{}, 错误信息：{}", enterpriseCode, e.getMessage());
            return ResponseData.Error(ReturnCodeEnum.SYSTEM_ERROR, e.getMessage());
        }
    }

    /**
     * 删除企业拓扑图
     * 
     * @param enterpriseCode 企业编码
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Response deleteTopology(@RequestParam("enterpriseCode") String enterpriseCode) {
        log.info("删除企业拓扑图，企业编码：{}", enterpriseCode);
        try {
            businessTopologyService.deleteTopologyByEnterpriseCode(enterpriseCode);
            log.info("拓扑图删除成功，企业编码：{}", enterpriseCode);
            return ResponseData.OK();
        } catch (RuntimeException e) {
            log.error("删除企业拓扑图失败，企业编码：{}, 错误信息：{}", enterpriseCode, e.getMessage());
            return ResponseData.Error(ReturnCodeEnum.SYSTEM_ERROR, e.getMessage());
        }
    }
}